#include "MBAlbumComparison.hpp"

void MBAlbumComparison::setAlbum(std::string v) {
    rf->title(v);
    rf->discId("");
}
void MBAlbumComparison::setAlbumID(std::string v) {
    rf->title("");
    rf->discId(v);
}
void MBAlbumComparison::setArtist(std::string v) {
    rf->artistName(v);
    rf->artistId("");
}
void MBAlbumComparison::setArtistID(std::string v) {
    rf->artistName("");
    rf->artistId(v);
}
//void MBAlbumComparison::setType(unsigned short v) {type=v;}
void MBAlbumComparison::setTracks(unsigned short v) {tracks=v;}
void MBAlbumComparison::setCDs(unsigned char v) {cds=v;}

MBAlbumComparison::MBAlbumComparison() {
    /*std::string    	host = "musicbrainz.org";
    int  	        port = 80;
    std::string   	pathPrefix = "/ws";
    std::string   	username = std::string();
    std::string   	password = std::string();
    std::string   	realm = "musicbrainz.org";
    ws=MusicBrainz::WebService::WebService(host, port, pathPrefix, username, password, realm);*/
    /*
    Release::TYPE_ALBUM
    Release::TYPE_OFFICIAL
    Release::TYPE_LIVE
    Release::TYPE_COMPILATION
    ...
    */
    ws=new MusicBrainz::WebService();
    q=new MusicBrainz::Query();
    af=new MusicBrainz::ArtistFilter();
    rf=new MusicBrainz::ReleaseFilter();
    tf=new MusicBrainz::TrackFilter();
    af->name("Nightwish");
    rf->releaseType(MusicBrainz::Release::TYPE_OFFICIAL);
    al=q->getArtists(af);
    std::cout<<std::endl<<std::endl;
    MusicBrainz::Artist *a=NULL;
    MusicBrainz::Release *r=NULL;
    QString aid, rid;
    for(unsigned int i=0; i<al.size(); i++) {
        a=al[i]->getArtist();
        aid=a->getId().substr(a->getId().find_last_of("/")+1).c_str();
        std::cout<<a->getName()<<"\t  ("<<al[i]->getScore()<<"%)"<<std::endl;
        rf->artistId(aid.toStdString());
        rl=q->getReleases(rf);
        for(unsigned int j=0; j<rl.size(); j++) {
            r=rl[j]->getRelease();
            rid=r->getId().substr(r->getId().find_last_of("/")+1).c_str();
            ///FIXME: get the number of CD
            std::cout<<"   "<<r->getReleaseEvent(0)->getDate()<<"   "<<r->getTitle()<<"\t  ("<<r->getNumDiscs()<<" CD / "<<r->getTracksCount()<<" tracks / "<<rl[j]->getScore()<<"%)"<<std::endl;
        }
    }
    delete a;
    delete r;
}
MBAlbumComparison::~MBAlbumComparison() {
    delete af;
    delete rf;
}

std::vector<MusicBrainz::ArtistResult*>  MBAlbumComparison::getArtists()  {return al;}
std::vector<MusicBrainz::ReleaseResult*> MBAlbumComparison::getReleases() {return rl;}
std::vector<MusicBrainz::TrackResult*>  MBAlbumComparison::getTracks()    {return tl;}

void MBAlbumComparison::searchArtist() {
    af->name(artist);
    al=q->getArtists(af);
}

void MBAlbumComparison::search() {
    if(!artist.empty()) { //search suiting artist
        searchArtist();
    } else if(!albumID.empty()) { //search album from an album mbID
        rl.clear();
        MusicBrainz::Release *rel=q->getReleaseById(albumID);
        if(rel!=NULL) {
            rl.push_back(new MusicBrainz::ReleaseResult(rel, 100));
        }
    } else if(!artistID.empty() && !album.empty() && albumID.empty()) { //search artist albums from the album title and artist mbID
        rf->artistId(artistID);
        rf->discId("");
        rf->title(album);
        rl=q->getReleases(rf);
        ///TODO: CD type support (album, compilation, soundtrack, live, ...) (else too)
    } else if(!artistID.empty() && albumID.empty() && album.empty()) { //search album with this artist
        rf->artistId(artistID);
        rf->discId("");
        rf->title("");
        rl=q->getReleases(rf);
    }
}

void MBAlbumComparison::searchTrack() {
    tf->artistId(artistID).artistName(artist).releaseId(albumID).releaseTitle(album).title(track);
    tl=q->getTracks(tf);
}

void MBAlbumComparison::set(unsigned int k, bool r) {
    for(unsigned int i=0; i<k && i<remote[k].size(); k++) {
        //(*local[i])=(*remote[k][i]);
        //(*local[i]).rename("");
        local[i]->updateDB();
    }
}
